package ca.ets.osgi.uiservice;

import java.util.ArrayList;

import ca.ets.osgi.UIinterface.IUIService;
import ca.ets.osgi.uiservice_controller.ControllerMainService;
import ca.ets.osgi.uiservice_model.ModeleMainService;
import ca.ets.osgi.uiservice_view.ViewMainService;

public class UIService implements IUIService {

	//private attribute
	private ViewMainService view;
	private ModeleMainService modele;
	private ControllerMainService controller;
	private boolean methode1_on = true;
	private boolean methode2_on = true;
	private Thread t;
	private boolean isService_on = true;
	private String userRequest;
	
	/**
	 * CONSTRUCTOR
	 * Set the view, the modele and the controller
	 * then start the thread
	 */
	public UIService(){}

	@Override
	public Object getUserCharateristicsofRoom() {
		System.err.println("[Getting user information]");
		resetAll();
		startAll();
		//startRunnable();
		//t.currentThread().start();
		//t.s
		this.isService_on =true;
		Object obj = null;
		while(this.methode1_on){
			if(controller.isUserRequestReady()==true){
				this.methode1_on = false;
				ArrayList<Object> list = new ArrayList<Object>();
				list.add(modele.getUser().getSurface());
				list.add(modele.getUser().isProjectorAvailable());
				list.add(modele.getUser().getDate());
				list.add(modele.getUser().getHourStart());
				list.add(modele.getUser().getHourEnd());
				obj = list;
				System.out.println("The user criterion was properly capture");
				System.out.println("");
				this.userRequest = modele.userToString2();
			}
		
		}
		return obj;
	}

	@Override
	public Object getUserRoomChoice(ArrayList<Object> object) {
		System.out.println("[Showing List of Available Rooms]");
		controller.isUserRequestReady =false;
		Object obj = null;
		view.getViewchoice().setJLabelRequest(userRequest);
		modele.setListRoomsInfo(object);
		while(this.methode2_on){
			if(controller.isUserResponseReady()==true){
				this.methode2_on = false;
				ArrayList<Object> list = new ArrayList<Object>();
				list.add(modele.getUser().getReserver());
				list.add(modele.getUser().getRoomID());
				list.add(modele.getUser().getDate());
				list.add(modele.getUser().getHourStart());
				list.add(modele.getUser().getHourEnd());
				obj = list;
				System.out.println("The user choice was properly taking");
				System.out.println("");
			}
			
		}

		
		
		// TODO Auto-generated method stub
		return obj;
	}

	@Override
	public Object getUserRoomChoic(Object object) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * Reset the view(closing it), the modele and the controller
	 */
	private void resetAll(){
		if(this.isService_on ==true && view != null && modele != null && controller != null){
			this.t = null;
			view.closeView();			
			modele = null;
			view = null;
			controller = null;
			this.isService_on = false; 
			this.methode1_on = true;
			this.methode2_on = true;
		}
	}
	
	/**
	 * Start the view, the modele(adding the view as an Observer) and the controller(adding the modele and the view)
	 */
	private void startAll(){
		view = new ViewMainService();
		modele = new ModeleMainService(view);
		controller = ControllerMainService.getInstance();
		controller.setModeleAndView(modele,view);
		//controller.run();
		t = new Thread(controller);
		t.start();
	}

	@Override
	public void showConfirmation(Object object) {
		System.out.println("[Showing confirmation]");
		modele.setUserInfo((ArrayList<Object>) object);
		
		while(true){
			if(controller.isTerminated()== true);
			System.out.println("PROGRAM TERMINATED!!!!!");
			break;
		}
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		
	}
	
	

}
